#include "mainwindow.h"
#include <QApplication>

#include <QStringList>
#include <QString>
#include <QProcess>
#include <QDebug>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
void startCrashReport() {
    QStringList args;
    QProcess process;
    qInfo() << "start crash report!";
#ifdef Q_OS_WIN
    process.startDetached(QApplication::applicationDirPath() + "/Crashreporter.exe", args);
#else
    process.startDetached(QApplication::applicationDirPath() + "/Crashreporter", args);
#endif
}

#ifdef Q_OS_WIN
LONG WINAPI ApplicationCrashHandler(EXCEPTION_POINTERS *pException) {
    startCrashReport();
    return EXCEPTION_EXECUTE_HANDLER;
}

void setExceptionFunciton()
{
    // 注册异常处理函数
    LPTOP_LEVEL_EXCEPTION_FILTER Top = SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler);
}
#else
#include "objectiveapi.h"
void UncaughtExceptionHandler(NSException *exception) {
    startCrashReport();
}

void HandleException(int signo)
{
    startCrashReport();
    (void) signal(signo, SIG_DFL);
}

void setExceptionFunciton()
{
    // 注册异常处理函数
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
    //捕获常用的错误信息，段错误，内存无效，bus，管道等等
    signal(SIGABRT, HandleException);
    signal(SIGILL, HandleException);
    signal(SIGSEGV, HandleException);
    signal(SIGFPE, HandleException);
    signal(SIGBUS, HandleException);
    signal(SIGPIPE, HandleException);
}
#endif


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    setExceptionFunciton();
    MainWindow w;
    w.show();
    return a.exec();
}
